package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;

import dao.ReplysDao;
import dao.UsersDao;
import dao.pojo.Replys;
import dao.pojo.Topics;
import dao.pojo.Users;

public class ReplysDaoImpl extends BaseDaoImpl implements ReplysDao {
	private UsersDao usersDao;

	protected UsersDao getUsersDao() {
		return usersDao;
	}

	public void setUsersDao(UsersDao usersDao) {
		this.usersDao = usersDao;
	}

	public void addReplys(Replys replys) {
		Connection conn=getPool().getConnection();
		
		PreparedStatement pstmt=null;
		String sql="INSERT INTO replys(user_id,submit_time,contents,parent) VALUES(?,?,?,?)";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, replys.getUser().getId());
			pstmt.setTimestamp(2, new Timestamp(replys.getSubmitTime().getTime()));
			pstmt.setString(3, replys.getContent());
			pstmt.setInt(4, replys.getTopic().getId());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			getPool().freeConnection(conn);
		}
	}
	
	public void saveReplys(Replys replys) {
		Connection conn=getPool().getConnection();
		
		PreparedStatement pstmt=null;
		String sql="UPDATE replys SET contents=? WHERE id=?";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, replys.getContent());
			pstmt.setInt(2, replys.getId());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			getPool().freeConnection(conn);
		}
		
	}

	public void deleteAllReplysByTopics(Topics topic) {
		Connection conn=getPool().getConnection();
		
		PreparedStatement pstmt=null;
		String sql="DELETE FROM replys WHERE parent=?";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, topic.getId());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			getPool().freeConnection(conn);
		}
		
	}

	public void deleteAllReplysByUsers(Users user) {
		Connection conn=getPool().getConnection();
		
		PreparedStatement pstmt=null;
		String sql="DELETE FROM replys WHERE user_id=?";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, user.getId());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			getPool().freeConnection(conn);
		}
	}

	public void deleteReplys(Replys reply) {
		Connection conn=getPool().getConnection();
		
		PreparedStatement pstmt=null;
		String sql="DELETE FROM replys WHERE user_id=?";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, reply.getId());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			getPool().freeConnection(conn);
		}
		
	}

	public ArrayList<Replys> getAllReplysByTopic(Topics topic) {
		Connection conn=getPool().getConnection();
		
        ArrayList<Replys> replysList=new ArrayList<Replys>();
        String sql = "SELECT * FROM replys WHERE parent=?";
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, topic.getId());
            rs = pstmt.executeQuery();
            while(rs.next()){
            	Replys reply=new Replys();
            	reply.setId(rs.getInt("id"));
            	reply.setContent(rs.getString("contents"));
            	Users user=new Users();
            	user.setId(rs.getInt("user_id"));
                reply.setUser(usersDao.getUsersById(user));
                reply.setTopic(topic);
                reply.setSubmitTime(rs.getTimestamp("submit_time"));
                replysList.add(reply);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
			getPool().freeConnection(conn);
		}
        return replysList;
	}

}
